package com.cool.modules.base.service.impl;

import cn.hutool.json.JSONObject;
import com.cool.core.base.BaseServiceImpl;
import com.cool.core.util.TenantUtil;
import com.cool.modules.base.dto.sys.TenantInfoDto;
import com.cool.modules.base.entity.sys.BaseSysDepartmentEntity;
import com.cool.modules.base.entity.sys.BaseSysTenantManagerUserEntity;
import com.cool.modules.base.entity.sys.BaseSysUserEntity;
import com.cool.modules.base.mapper.BaseSysTenantManagerUserMapper;
import com.cool.modules.base.service.BaseSysTenantManagerUserService;
import com.cool.modules.base.service.sys.BaseSysDepartmentService;
import com.cool.modules.base.service.sys.BaseSysUserService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 租户管理员关系
 */
@Service
@AllArgsConstructor
public class BaseSysTenantManagerUserServiceImpl extends BaseServiceImpl<BaseSysTenantManagerUserMapper,
        BaseSysTenantManagerUserEntity> implements BaseSysTenantManagerUserService {

    private final BaseSysUserService baseSysUserService;

    private final BaseSysDepartmentService baseSysDepartmentService;

    @Override
    public List<Long> getTenantIdsByUserId(Long userId) {
        List<BaseSysTenantManagerUserEntity> list = getTenantManagerUsersByUserId(userId);
        return list.stream().map(BaseSysTenantManagerUserEntity::getManagerTenantId).toList();
    }

    @Override
    public List<BaseSysTenantManagerUserEntity> getTenantManagerUsersByUserId(Long userId) {
        return this.list(QueryWrapper.create().eq(BaseSysTenantManagerUserEntity::getUserId, userId));
    }

    @Override
    public Object page(JSONObject requestParams, Page<BaseSysTenantManagerUserEntity> page, QueryWrapper queryWrapper) {
        Long[] tenantIds = TenantUtil.getTenantIds();
        Page<BaseSysDepartmentEntity> deptPage =
                baseSysDepartmentService.pageByTenantIds(page.getPageNumber(), page.getPageSize(), tenantIds);
        List<TenantInfoDto> tenantInfoDtoList = new ArrayList<>();
        deptPage.getRecords().forEach(dept -> {
            TenantInfoDto tenantInfoDto = new TenantInfoDto();
            // 租户信息
            tenantInfoDto.setTenantName(dept.getName());
            tenantInfoDto.setId(dept.getId());
            // 获取对应的用户列表
            List<BaseSysTenantManagerUserEntity> tenantManagerList =
                    this.list(QueryWrapper.create().eq(BaseSysTenantManagerUserEntity::getManagerTenantId, dept.getId()));
            List<Long> list = tenantManagerList.stream().map(BaseSysTenantManagerUserEntity::getUserId).toList();
            List<BaseSysUserEntity> userList = new ArrayList<>();
            if(!list.isEmpty()){
                userList = baseSysUserService.list(QueryWrapper.create().in(BaseSysUserEntity::getId, list));
            }
            tenantInfoDto.setManagerList(userList);
            tenantInfoDtoList.add(tenantInfoDto);
        });
        Page<TenantInfoDto> pageDto = new Page<>(page.getPageNumber(), page.getPageSize());
        pageDto.setRecords(tenantInfoDtoList);
        pageDto.setTotalPage(deptPage.getTotalPage());
        pageDto.setTotalRow(deptPage.getTotalRow());
        return pageDto;
    }

    @Override
    public Object info(Long id) {
        BaseSysDepartmentEntity dept = baseSysDepartmentService.getById(id);
        TenantInfoDto tenantInfoDto = new TenantInfoDto();
        tenantInfoDto.setId(dept.getId());
        tenantInfoDto.setTenantName(dept.getName());
        List<BaseSysTenantManagerUserEntity> tenantManagerList =
                this.list(QueryWrapper.create().eq(BaseSysTenantManagerUserEntity::getManagerTenantId, dept.getId()));
        List<Long> list = tenantManagerList.stream().map(BaseSysTenantManagerUserEntity::getUserId).toList();
        List<BaseSysUserEntity> userList = new ArrayList<>();
        if(!list.isEmpty()){
            userList = baseSysUserService.list(QueryWrapper.create().in(BaseSysUserEntity::getId, list));
        }
        tenantInfoDto.setManagerList(userList);
        return tenantInfoDto;
    }
}